package com.me.mysqladmin.controller;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.me.mysqladmin.entity.Location;
import com.me.mysqladmin.entity.TableData;
import com.me.mysqladmin.service.TableService;
import com.me.mysqladmin.util.EasyUIFormat;

@Controller
@RequestMapping("/tableData")
public class TableDataController extends BaseController {
	
	// 注入业务组件
	@Resource
	private TableService tableService;
	
	/**
	 * 跳转到主页
	 */
	@RequestMapping("/index.do")
	public String toIndex(){
		return "main/tableData";
	}
	
	/**
	 * 跳转到插入/修改数据页
	 */
	@RequestMapping("/editPage.do")
	public String toedit(String row, Model model){
		LOG.info("--------编辑记录：" + row + "--------");
		if(row != null && row != ""){
			model.addAttribute("selectedRow", row);
		}
		return "extra/editTableData";
	}
	
	
	/**
	 * 查询一张表的记录
	 */
	@RequestMapping("/queryPage.do")
	@ResponseBody
	public Map<String, Object> queryAll(Location loc){
		if(loc.getDatabaseName() == null || "".equals(loc.getDatabaseName().trim())){
			return EasyUIFormat.parseGrid(Collections.emptyList(), LinkedHashMap.class);
		}
		if(loc.getTableName() == null || "".equals(loc.getTableName().trim())){
			return EasyUIFormat.parseGrid(Collections.emptyList(), LinkedHashMap.class);
		}
		List<Map<String, Object>> datas = tableService.showTableDatas(loc);
		return EasyUIFormat.parseGrid(datas, LinkedHashMap.class);
	}
	
	/**
	 * 插入一条记录
	 */
	@RequestMapping("/insert.do")
	@ResponseBody
	public Map<String, Object> insert(TableData tableData){
		try {
			LOG.info("--------插入记录：" + tableData.getMap() + "--------");
			Integer line = tableService.insertTableData(tableData);
			return messageSuccess(line);
		} catch (Exception e) {
			return messageFailure(e.getMessage(), "插入记录失败！");
		}
	}
	
	/**
	 * 修改一条记录
	 */
	@RequestMapping("/modify.do")
	@ResponseBody
	public Map<String, Object> modify(TableData tableData){
		try {
			LOG.info("--------修改记录：" + tableData.getMap() + "--------");
			Integer line = tableService.modifyTableData(tableData);
			return messageSuccess(line);
		} catch (Exception e) {
			return messageFailure(e.getMessage(), "插入记录失败！");
		}
	}
	
	/**
	 * 获得表的主键名
	 */
	@RequestMapping("/getPkName.do")
	@ResponseBody
	public Map<String, Object> getPkName(Location loc){
		try {
			LOG.info("--------当前位置：" + loc + "--------");
			String pkName = tableService.getPrimaryKey(loc);
			return messageSuccess(pkName);
		} catch (Exception e) {
			return messageFailure(e.getMessage(), "获取主键失败！");
		}
	}
	
	/**
	 * 删除一条记录
	 */
	@RequestMapping("/delete.do")
	@ResponseBody
	public Map<String, Object> delete(TableData tableData){
		try {
			LOG.info("--------删除记录：" + tableData.getMap() + "--------");
			Integer line = tableService.deleteTableData(tableData);
			return messageSuccess(line);
		} catch (Exception e) {
			return messageFailure(e.getMessage(), "删除记录失败！");
		}
	}
}